package com.umfwechat.util.email;

import com.umfwechat.core.loggers.LoggerDetail;
import org.slf4j.Logger;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2017/5/16.
 */
public class SendEmailThread implements Runnable{
    private final static Logger LOGGER_DETAIL = LoggerDetail.LOGGER_DETAIL();
    List<File> fileList;
    String title;
    StringBuilder mBody;
    String mailto;
    public SendEmailThread(List<File> fileList, String title, StringBuilder mBody, String mailto){
        this.fileList = fileList;
        this.title = title;
        this.mBody = mBody;
        this.mailto = mailto;
    }
    
    public SendEmailThread(String title, StringBuilder mBody, String mailto){
        this.title = title;
        this.mBody = mBody;
        this.mailto = mailto;
    }

    int sendCounts = 0;

    public void run() {
        try {
            LOGGER_DETAIL.info("发送邮件准备...");
            SendMailUtil.sendMail(mailto, title, mBody.toString(), fileList);
            if(fileList!=null&&fileList.size()>0){
                for(File f : fileList){
                    f.delete();
                }
            }
            LOGGER_DETAIL.info("发送邮件完成...");
        } catch (Exception e) {
            sendCounts ++;
            LOGGER_DETAIL.error("发送邮件失败",e);
            if(sendCounts<10){
                try {
                    Thread.sleep(60*1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                run();
            }
        }
    }
}
